import sys;
from PyQt4 import QtGui, QtCore; #@UnusedImport

class MyTableWidget(QtGui.QTableWidget):
    def __init__(self, parent=None):
        super(MyTableWidget, self).__init__(parent=None);
    
    def keyPressEvent(self, event):
        super(MyTableWidget, self).keyPressEvent(event);
        #Copy Selected Cells
        if (event.matches(QtGui.QKeySequence.Copy)):
            selectedItems = self.selectedItems();
            selectedRanges = self.selectedRanges();
            nRows, nCols, minRow, minCol, maxRow, maxCol = self.getRowsCols(selectedRanges); #@UnusedVariable
            clipBoard = QtGui.QApplication.clipboard();
            sText = [["\t" for i in range(nCols)] for j in range(nRows)]; #@UnusedVariable
            for item in selectedItems:
                sText[item.row()-minRow][item.column()-minCol] = str(item.data(0).toString()) + "\t";
            for i in range(nRows):
                sText[i][nCols-1] += "\n";
            sData = "";
            for iRow in range(len(sText)):
                for iCol in range(len(sText[0])):
                    sData += sText[iRow][iCol];
#            print sData;
            clipBoard.setText(sData);
        #Paste Cells
        if (event.matches(QtGui.QKeySequence.Paste)):
            selectedItems = self.selectedItems();
            selectedRanges = self.selectedRanges();
            nRows, nCols, minRow, minCol, maxRow, maxCol = self.getRowsCols(selectedRanges); #@UnusedVariable
            clipBoard = QtGui.QApplication.clipboard();
            sTextRows = str(clipBoard.text()).splitlines();
#            print minRow, minCol;
            for i in range(len(sTextRows)):
                sTextCols = sTextRows[i].split("\t");
                if minRow+i < self.rowCount():
                    for j in range(len(sTextCols)):
                        if sTextCols[j]!="" and minCol+j < self.columnCount():
                            #qCell = QtGui.QTableWidgetItem(sTextCols[j] + ": " + str(i) + "," + str(j));
                            qCell = QtGui.QTableWidgetItem(sTextCols[j]);
                            self.setItem(minRow+i, minCol+j, qCell);
        #Delete Cells Content
        if (event.matches(QtGui.QKeySequence.Delete)):
            for item in self.selectedItems():
                qCell = QtGui.QTableWidgetItem("");
                self.setItem(item.row(), item.column(), qCell);
    
    def getRowsCols(self, selectedRanges):
        nMinRow, nMinCol = sys.maxint, sys.maxint;
        nMaxRow, nMaxCol = -sys.maxint, -sys.maxint;
        for itemRange in selectedRanges:
            if itemRange.topRow() < nMinRow:
                nMinRow = itemRange.topRow();
            if itemRange.bottomRow() > nMaxRow:
                nMaxRow = itemRange.bottomRow();
            if itemRange.leftColumn() < nMinCol:
                nMinCol = itemRange.leftColumn();
            if itemRange.rightColumn() > nMaxCol:
                nMaxCol = itemRange.rightColumn();
        nRows = (nMaxRow - nMinRow) + 1;
        nCols = (nMaxCol - nMinCol) + 1;
        return nRows, nCols, nMinRow, nMinCol, nMaxRow, nMaxCol;